package com.system.ad.index.adplan;

import com.system.ad.index.IndexAware;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 推广计划索引服务
 */
@Slf4j
@Component
public class AdPlanIndex implements IndexAware<Long, AdPlanVo> {
    private static Map<Long, AdPlanVo> indexMap = new ConcurrentHashMap<>();

    @Override
    public AdPlanVo get(Long key) {
        return indexMap.get(key);
    }

    @Override
    public void add(Long key, AdPlanVo value) {
        log.info("before add:{}", indexMap);
        indexMap.put(key, value);
        log.info("after add:{}", indexMap);
    }

    @Override
    public void update(Long key, AdPlanVo value) {
        log.info("before update:{}", indexMap);
        AdPlanVo adPlanVo = indexMap.get(key);
        if (adPlanVo == null) {
            indexMap.put(key, value);
        } else {
            adPlanVo.update(value);
        }
        log.info("after update:{}", indexMap);
    }

    @Override
    public void delete(Long key, AdPlanVo value) {
        log.info("before delete:{}", indexMap);
        indexMap.remove(key);
        log.info("after delete:{}", indexMap);
    }
}
